<?php
exit;
include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/album/inc.php");
$g_cgival = array();
$g_show = array();
$g_pro = array();

if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	
	cgi::both($g_cgival['uid'],"uid","");
	$g_cgival['uid'] = intval($g_cgival['uid']);
	
	cgi::both($g_cgival['p'],"p","");
	$g_cgival['p'] = intval($g_cgival['p']);
	
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	
	$uid = $g_cgival['uid'] ;
	$p = $g_cgival['p'] ;
	$pcount = 6;
	if($p < 1) $p=1;
	if(!$uid)
	{
		$user_info = new user_info();
		$is_login = $user_info->is_logined();
		if($is_login)
		{
			$user_detail_info = $user_info->get_cur_user_info();
			
			$uid = $user_detail_info["uid"];
			
		}
		if(!$uid)
		{
			throw new Exception("请先登录！");
		}
		
	}
	
	
	$db_album_info = new db_album_info();
	$count = $db_album_info->get_album_count_by_uid($uid);
	
	if($count)
	{
		$list = $db_album_info->get_album_list_by_uid($uid,$p,$pcount);
	}
	else
	{
		if($is_login)
		{	
			$title = "默认专辑";
			$id = $db_album_info->insert_album($title,"",$uid,$show_type,$body_url="",$body_attach="",$show_order="",$mblog_show_type="",$mblog_topic_id="",$mblog_topic="",$user_detail_info["user_nickname"],$user_detail_info["sex"],$user_detail_info["verified_type"]);
			$list[0] = array("id"=>$id,"title"=>$title,"desc"=>"");
		
		}
		
	}
	
	
	
	
	$g_show["data"]["selected_title"] = $list[0]["title"];
	$g_show["data"]["selected_id"] = $list[0]["id"];
		
	$g_show["data"]["list"] = $list;
	$g_show["data"]["count"] = $count;
	
	
	
	$page_total = intval(ceil($count/$pcount));
	
	$page_info = "<span class=\"cu_page\">{$p}</span> / <span class=\"all_page\">{$page_total}</span> ";
	
	if($p > 1)
	{
		$last_page = $p-1;
		$page_info .= "<a class=\"left r3\" href=\"javascript:;\" value='".$last_page."'>&lt;</a>";
	}
	if($p < $page_total)
	{
		$next_page = $p+1;
		$page_info .=  "<a class=\"right r3\" href=\"javascript:;\"  value='".$next_page."'>&gt;</a> ";
	}
	
	$g_show["data"]["page_info"] = $page_info;
	
	if($page_total > 1)
	{
		$g_show["data"]["show_page"] = "";
		$g_show["data"]["current_page"] =$p ;
		$g_show["data"]["total_page"] =$page_total;
		$g_show["data"]["last_page"] = $p-1 > 0 ? $p-1 :1;
		$g_show["data"]["next_page"] = $p+1 < $page_total ? $p+1 : $page_total;
	}
	else
	{
		$g_show["data"]["show_page"] = "style=display:none;";
	}
	
	
	
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "操作成功";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}